package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 获取营业数据
     * @return
     * {
     *   "code": 0,
     *   "data": {
     *     "newUsers": 0,   新增用户数
     *     "orderCompletionRate": 0,   订单完成率
     *     "turnover": 0,   营业额
     *     "unitPrice": 0,  平均客单价
     *     "validOrderCount": 0  有效订单数
     *   },
     *   "msg": "string"
     * }
     */
    @Override
    public BusinessDataVO businessData(LocalDateTime begin, LocalDateTime end) {
        // 获取新增用户数
            //获取今日结束时间

        Integer newUsers = userMapper.countByCreateTime(begin,end);
        // 获取有效订单数
        Integer validOrderCount = ordersMapper.countByTimeAndStatus(Orders.COMPLETED, begin , end);
        // 获取订单完成率
            //获取今天的总订单数
        Integer totalOrderCount = ordersMapper.countByTimeAndStatus(null,begin , end);
        Double CompletionRate = 0.0;
        if (totalOrderCount != 0 && totalOrderCount != 0) {
             CompletionRate = (double) validOrderCount / totalOrderCount;
        }
        // 获取营业额
        Double turnover = ordersMapper.totalAmount(begin,end, Orders.COMPLETED);
        if (turnover == null){
            turnover = 0.0;
        }
        // 获取平均客单价
        //营业额/有效订单数
        Double unitPrice = 0.0;
        if (totalOrderCount != 0 && validOrderCount != 0){
             unitPrice = turnover / validOrderCount;
        }

        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(CompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .build();
    }

    /**
     * 套餐总览
     * @return
     * {
     *   "code": 0,
     *   "data": {
     *     "discontinued": 0,
     *     "sold": 0
     *   },
     *   "msg": "string"
     * }
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        //根据套餐状态，分别获取起售和停售的数量
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(1);
        Integer sold = setmealMapper.list(setmeal).size();
        setmeal.setStatus(0);
        Integer Discontinued = setmealMapper.list(setmeal).size();
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(Discontinued)
                .build();
    }

    /**
     * 菜品总览
     * @return
     * {
     *   "code": 0,
     *   "data": {
     *     "discontinued": 0,
     *     "sold": 0
     *   },
     *   "msg": "string"
     * }
     */
    @Override
    public DishOverViewVO overviewDishes() {
        //根据菜品状态，分别获取起售和停售的数量
        Integer sold = dishMapper.getNumByStatus(1);
        Integer Discontinued = dishMapper.getNumByStatus(0);
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(Discontinued)
                .build();
    }

    /**
     * 订单总览
     * @return
     * {
     *   "code": 0,
     *   "data": {
     *     "allOrders": 0,
     *     "cancelledOrders": 0,
     *     "completedOrders": 0,
     *     "deliveredOrders": 0,
     *     "waitingOrders": 0
     *   },
     *   "msg": "string"
     * }
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        Integer cancelledOrders = ordersMapper.countStatus(Orders.CANCELLED);
        Integer waitingOrders = ordersMapper.countStatus(Orders.TO_BE_CONFIRMED);
        Integer deliveredOrders = ordersMapper.countStatus(Orders.CONFIRMED);
        Integer completedOrders = ordersMapper.countStatus(Orders.COMPLETED);
        Integer allOrders = ordersMapper.countByTimeAndStatus(null,null,null);
        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }

}
